<?php
$server = new swoole_server("172.17.231.145", 8899);
$server->set(
    array(
        'open_eof_check'=>true, //是否检测结尾
        'package_eof'=>PHP_EOL, //结尾标识，这里的结尾最好使用不容易跟真正的body混淆的字符
        'open_eof_split'=>true //必须开启切割
    )
);
$server->on('connect', function ($server, $fd){
    echo "connection open: {$fd}\n";
});

$server->on('receive', function ($server, $fd, $reactor_id, $data) {
    //echo "当前处理的:{$fd},来自:{$fd},数据{$data}".PHP_EOL;
    $data = rtrim($data,PHP_EOL);

    $info = json_decode($data, true);
    if($info){
        if($info["type"]  > 0 && $info["type"] != 33 ){
            echo $data;
            $cli = new swoole_http_client('127.0.0.1', 80);
            $cli->post('http://weixian.hbweiyinqing.cn/api/matrix/wx_member/add_matrix_member.html', array("username" => $info["username"],"nickname"=>$info["nickname"],"type"=>$info["type"]), function ($cli) {
                echo "Length: " . strlen($cli->body) . PHP_EOL;
                echo $cli->body.PHP_EOL;
            });
        }
    }

});
$server->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});
$server->start();



/*class WebsocketTest {
    public $server;
    public function __construct() {
        $this->server = new swoole_websocket_server("172.17.231.145", 8899);
        $this->server->on('open', function (swoole_websocket_server $server, $request) {
            echo "Socket启动{$request->fd}\n";
        });
        $this->server->on('message', function (swoole_websocket_server $server, $frame) {


            echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
            //$server->push($frame->fd, "this is server");
        });
        $this->server->on('close', function ($ser, $fd) {
            echo "Socket结束 {$fd} \n";

        });

        $this->server->start();
    }
}
new WebsocketTest();*/